home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / viewZoomOverlay.js < prev    next >
Encoding:
JavaScript  |  2005-02-01  |  3.6 KB  |  152 lines

  1. //@line 40 "/c/mozilla/toolkit/content/viewZoomOverlay.js"
  2.  
  3. /** Document Zoom Management Code
  4.  *
  5.  * To use this, you'll need to have a getMarkupDocumentViewer() function which returns a
  6.  * nsIMarkupDocumentViewer.
  7.  *
  8.  **/
  9.  
  10.  
  11. function ZoomManager() {
  12. }
  13.  
  14. ZoomManager.prototype = {
  15.   instance : null,
  16.  
  17.   getInstance : function() {
  18.     if (!ZoomManager.prototype.instance)
  19.       ZoomManager.prototype.instance = new ZoomManager();
  20.  
  21.     return ZoomManager.prototype.instance;
  22.   },
  23.  
  24.   MIN : 1,
  25.   MAX : 2000,
  26.   factorOther : 300,
  27.   factorAnchor : 300,
  28.     zoomFactors: [50, 75, 90, 100, 120, 150, 200],
  29.   steps : 0,
  30.  
  31.   get textZoom() {
  32.     var currentZoom;
  33.     try {
  34.       currentZoom = Math.round(getMarkupDocumentViewer().textZoom * 100);
  35.       if (this.indexOf(currentZoom) == -1) {
  36.         if (currentZoom != this.factorOther) {
  37.           this.factorOther = currentZoom;
  38.           this.factorAnchor = this.factorOther;
  39.         }
  40.       }
  41.     } catch (e) {
  42.       currentZoom = 100;
  43.     }
  44.     return currentZoom;
  45.   },
  46.  
  47.   set textZoom(aZoom) {
  48.     if (aZoom < this.MIN || aZoom > this.MAX)
  49.       throw Components.results.NS_ERROR_INVALID_ARG;
  50.  
  51.     getMarkupDocumentViewer().textZoom = aZoom / 100;
  52.   },
  53.  
  54.   enlarge : function() {
  55.     this.jump(1);
  56.   },
  57.  
  58.   reduce : function() {
  59.     this.jump(-1);
  60.   },
  61.   reset : function() {
  62.     this.textZoom = 100;
  63.   },
  64.   indexOf : function(aZoom) {
  65.     var index = -1;
  66.     if (this.isZoomInRange(aZoom)) {
  67.       index = this.zoomFactors.length - 1;
  68.       while (index >= 0 && this.zoomFactors[index] != aZoom)
  69.         --index;
  70.     }
  71.  
  72.     return index;
  73.   },
  74.  
  75.   /***** internal helper functions below here *****/
  76.  
  77.   isZoomInRange : function(aZoom) {
  78.     return (aZoom >= this.zoomFactors[0] && aZoom <= this.zoomFactors[this.zoomFactors.length - 1]);
  79.   },
  80.  
  81.   jump : function(aDirection) {
  82.     if (aDirection != -1 && aDirection != 1)
  83.       throw Components.results.NS_ERROR_INVALID_ARG;
  84.  
  85.     var currentZoom = this.textZoom;
  86.     var insertIndex = -1;
  87.     const stepFactor = 1.5;
  88.  
  89.     // temporarily add factorOther to list
  90.     if (this.isZoomInRange(this.factorOther)) {
  91.       insertIndex = 0;
  92.       while (this.zoomFactors[insertIndex] < this.factorOther)
  93.         ++insertIndex;
  94.  
  95.       if (this.zoomFactors[insertIndex] != this.factorOther)
  96.         this.zoomFactors.splice(insertIndex, 0, this.factorOther);
  97.     }
  98.  
  99.     var factor;
  100.     var done = false;
  101.  
  102.     if (this.isZoomInRange(currentZoom)) {
  103.       var index = this.indexOf(currentZoom);
  104.       if (aDirection == -1 && index == 0 ||
  105.           aDirection ==  1 && index == this.zoomFactors.length - 1) {
  106.         this.steps = 0;
  107.         this.factorAnchor = this.zoomFactors[index];
  108.       } else {
  109.         factor = this.zoomFactors[index + aDirection];
  110.         done = true;
  111.       }
  112.     }
  113.  
  114.     if (!done) {
  115.       this.steps += aDirection;
  116.       factor = this.factorAnchor * Math.pow(stepFactor, this.steps);
  117.       if (factor < this.MIN || factor > this.MAX) {
  118.         this.steps -= aDirection;
  119.         factor = this.factorAnchor * Math.pow(stepFactor, this.steps);
  120.       }
  121.       factor = Math.round(factor);
  122.       if (this.isZoomInRange(factor))
  123.         factor = this.snap(factor);
  124.       else
  125.         this.factorOther = factor;
  126.     }
  127.  
  128.     if (insertIndex != -1)
  129.       this.zoomFactors.splice(insertIndex, 1);
  130.  
  131.     this.textZoom = factor;
  132.   },
  133.  
  134.   snap : function(aZoom) {
  135.     if (this.isZoomInRange(aZoom)) {
  136.       var level = 0;
  137.       while (this.zoomFactors[level + 1] < aZoom)
  138.         ++level;
  139.  
  140.       // if aZoom closer to [level + 1] than [level], snap to [level + 1]
  141.       if ((this.zoomFactors[level + 1] - aZoom) < (aZoom - this.zoomFactors[level]))
  142.         ++level;
  143.  
  144.       aZoom = this.zoomFactors[level];
  145.     }
  146.  
  147.     return aZoom;
  148.   }
  149. }
  150.  
  151.  
  152.